﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace AF_Lucky
{
    public class LuckyCenter
    {
        private string _LuckyCenterUrl = "http://lucky.appdream.net/dataapi.ashx";
        public string LuckyCenterUrl
        {
            get
            {
                string configUrl = System.Configuration.ConfigurationManager.AppSettings["LuckyCenterUrl"] != null ? System.Configuration.ConfigurationManager.AppSettings["LuckyCenterUrl"].ToString() :"";
                if (!string.IsNullOrEmpty(configUrl))
                {
                    _LuckyCenterUrl = configUrl.Trim();
                }
                return _LuckyCenterUrl;
            }
        }

        public string getLuckyUrl(Guid ActionID, string OpenID, string UserIP)
        {
            return string.Format("{0}?Module=gift&Action=try&ActionID={1}&OpenID={2}&UserIP={3}", LuckyCenterUrl, ActionID, OpenID, UserIP);
        }

        public string getRegUrl(Guid ActionID)
        {
            return string.Format("{0}?Module=player&Action=reg&ActionID={1}", LuckyCenterUrl, ActionID);
        }

        public LuckyResult Lucky(Guid ActionID, string OpenID, string PlayerIP)
        {
            LuckyResult result = new LuckyResult()
            {
                ActionID = ActionID,
                GiftID = Guid.Empty,
                GiftName = "",
                IsSuccess = false,
                Message = "未知错误",
                OpenID = OpenID,
                RemainDayGiftCount = 0,
                RemainDayPlayCount = 0,
                RemainGiftCount = 0,
                RemainPlayCount = 0,
                UserIP = PlayerIP
            };
            try
            {
                using (WebClient webClient = new WebClient())
                {
                    webClient.Encoding = Encoding.UTF8;
                    string requestUrl = getLuckyUrl(ActionID, OpenID, PlayerIP);
                    string jsonResult = webClient.DownloadString(requestUrl);
                    result = Newtonsoft.Json.JsonConvert.DeserializeObject<LuckyResult>(jsonResult);
                }
                return result;
            }
            catch (Exception e)
            {
                result.Message = "严重错误:" + e.Message;
            }
            return result;
        }

        public PlayerRegResult RegUser(PlayerInfo playerInfo)
        {
            PlayerRegResult result = new PlayerRegResult()
            {
                IsSuccess = false,
                Message = "未知错误",
                PlayerID = Guid.Empty
            };
            try
            {
                if (playerInfo != null)
                {
                    NameValueCollection nvPlayInfo = new NameValueCollection();
                    nvPlayInfo.Add("ActionID", playerInfo.ActionID.ToString());
                    nvPlayInfo.Add("Address", playerInfo.Address);
                    nvPlayInfo.Add("City", playerInfo.City);
                    nvPlayInfo.Add("Id", playerInfo.Id.ToString());
                    nvPlayInfo.Add("EMail", playerInfo.EMail);
                    nvPlayInfo.Add("HeadImg", playerInfo.HeadImg);
                    nvPlayInfo.Add("ActionID", playerInfo.Id.ToString());
                    nvPlayInfo.Add("IP", playerInfo.IP);
                    nvPlayInfo.Add("Name", playerInfo.Name);
                    nvPlayInfo.Add("NickName", playerInfo.NickName);
                    nvPlayInfo.Add("OpenID", playerInfo.OpenID);
                    nvPlayInfo.Add("Other1", playerInfo.Other1);
                    nvPlayInfo.Add("Other2", playerInfo.Other2);
                    nvPlayInfo.Add("Other3", playerInfo.Other3);
                    nvPlayInfo.Add("Other4", playerInfo.Other4);
                    nvPlayInfo.Add("Other5", playerInfo.Other5);
                    nvPlayInfo.Add("Phone", playerInfo.Phone);
                    nvPlayInfo.Add("Province", playerInfo.Province);
                    nvPlayInfo.Add("QQ", playerInfo.QQ);
                    nvPlayInfo.Add("Sex", playerInfo.Sex);
                    nvPlayInfo.Add("WeiXin", playerInfo.WeiXin);


                    using (WebClient webClient = new WebClient())
                    {
                        string requestUrl = getRegUrl(playerInfo.ActionID);
                        byte[] tmp = webClient.UploadValues(requestUrl, "POST", nvPlayInfo);
                        string jsonResult = Encoding.UTF8.GetString(tmp);
                        result = Newtonsoft.Json.JsonConvert.DeserializeObject<PlayerRegResult>(jsonResult);
                    }
                    return result;
                }
            }
            catch (Exception e)
            {
                result.Message = "严重错误:" + e.Message;
            }
            return result;
        }
    }
}
